package com.canplay.screen.utils

import com.google.gson.Gson
import java.io.Reader
import java.lang.reflect.Type
import com.google.gson.reflect.TypeToken

object GsonUtils {

    private val gson: Gson by lazy{ Gson()}

    fun toJson(any: Any?): String {
        return gson.toJson(any)
    }

    fun <T>fromJson(json:String?,typeClass:Class<T>):T?{
        return gson.fromJson(json,typeClass)
    }

    fun <T>fromJson(json:String?,type: Type):T?{
        return gson.fromJson(json,type)
    }

    fun <T>fromJson(reader: Reader,type: Type):T?{
        return gson.fromJson(reader,type)
    }

    fun <T>fromJson(reader: Reader,typeClass:Class<T>):T?{
        return gson.fromJson(reader,typeClass)
    }

    fun getListType(type: Type): Type? {
        return TypeToken.getParameterized(MutableList::class.java, type).type
    }

    fun getSetType(type: Type): Type? {
        return TypeToken.getParameterized(MutableSet::class.java, type).type
    }

    fun getMapType(keyType: Type, valueType: Type): Type? {
        return TypeToken.getParameterized(MutableMap::class.java, keyType, valueType).type
    }

    fun getArrayType(type: Type): Type? {
        return TypeToken.getArray(type).type
    }


}